package org.jeecg.modules.engineering.service;

import feign.hystrix.FallbackFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 客户端的接口上的方法定义， 与服务器一样，
 */
@FeignClient(value = "CIM-BASEDATA", fallbackFactory=BaseDataClient.BaseDataClientFallBackFactory.class) //, fallback= BaseDataClient.BaseDataClientFallBack.class
public interface BaseDataClient {
    // 简单参数

    /**
     * @  RequestHeader("tenantId") Long tenantId是一类特殊参数， 能指导服务端选择对应的物业公司
     *        http://wiki.xinfu.info/pages/viewpage.action?pageId=1212783
     *  @param op
     * @param arg1
     * @param arg2
     * @param tenantId
     * @return
     */
    @RequestMapping(value = "/api/demo/simple",method = RequestMethod.POST)
    Result<Integer> simple(
            @RequestParam(name="op") String op,
            @RequestParam(name="arg1") Integer arg1,
            @RequestParam(name="arg2") Integer arg2, @RequestHeader("tenantId") Long tenantId);

    @RequestMapping(value = "/api/demo/hight",method = RequestMethod.POST)
    public Result<Object> hight(@RequestBody MyDate data) ;

    @Data
    class MyDate{
        Map<String, Object> innerObj = new HashMap<>();
        String arg1;
        String arg2;
    }

    @Slf4j
    @Component
    class BaseDataClientFallBackFactory implements FallbackFactory<BaseDataClient>{

        @Override
        public BaseDataClient create(Throwable throwable) {
            return new BaseDataClient(){

                @Override
                public Result<Integer> simple(String op, Integer arg1, Integer arg2, Long id) {
                    log.error("err", throwable);
                    return Result.error(1, throwable.getMessage());
                }

                @Override
                public Result<Object> hight(MyDate data) {
                    log.error("err", throwable);
                    return Result.error(1, throwable.getMessage());
                }
            };
        }
    }
}
